package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.BlogMapper;
import com.itheima.dao.CommentMapper;
import com.itheima.dao.CommentReportMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Comment;
import com.itheima.pojo.CommentReport;
import com.itheima.service.CommentService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentReportMapper commentReportMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private IdWorker idWorker;

    //获取评价列表
    @Override
    public PageResult<Comment> findAll(Map<String, Object> commentVO) {
        Integer currentPage = Integer.parseInt(commentVO.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(commentVO.get("pageSize").toString());
        String source = (String) commentVO.get("source");
        if (source.equals("all")){
            PageHelper.startPage(currentPage, pageSize);
            commentVO.remove("source");
            Example example = this.createExample(commentVO);
            Page<Comment> commentPage = (Page<Comment>) commentMapper.selectByExample(example);
            List<Comment> commentList = commentPage.getResult();
            for (Comment comment : commentList) {
                String uid = comment.getBlogUid();
                Blog blog = blogMapper.selectByPrimaryKey(uid);
                if(blog!=null){
                    comment.setBlog(blog);
                }
            }
            PageResult<Comment> pageResult=new PageResult<>();
            pageResult.setTotal(commentPage.getTotal());
            pageResult.setRecords(commentList);
            pageResult.setSize((long) commentPage.getPageSize());
            pageResult.setCurrent((long) commentPage.getPageNum());
            pageResult.setOptimizeCountSql(true);
            pageResult.setSearchCount(true);
            return pageResult;
        }else {
            throw new RuntimeException("查询失败");
        }

    }

    //编辑评论
    @Override
    @Transactional
    public void edit(Map<String, Object> commentVO) {

        //将map转为comment对象
        String commentStr = JSON.toJSONString(commentVO);
        Comment comment = JSON.parseObject(commentStr, Comment.class);
        comment.setUpdateTime(new Date());

        //修改操作
        commentMapper.updateByPrimaryKey(comment);
    }

    //删除评论
    @Override
    @Transactional
    public void delete(Map<String, Object> commentVO) {
        String uid = commentVO.get("uid").toString();
        commentMapper.deleteByPrimaryKey(uid);
    }

    //增加评论
    @Override
    @Transactional
    public void add(Map<String, Object> commentVO) {


        //将map转为comment对象
        String commentStr = JSON.toJSONString(commentVO);
        Comment comment = JSON.parseObject(commentStr, Comment.class);
        comment.setUid(String.valueOf(idWorker.nextId()));
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        //插入操作
        commentMapper.insertSelective(comment);
    }

    //批量删除评论
    @Override
    @Transactional
    public void deleteBatch(List<Map<String, Object>> commentVoList) {
        for (Map<String, Object> map : commentVoList) {
            String uid = map.get("uid").toString();
            commentMapper.deleteByPrimaryKey(uid);
        }
    }

    //获取评论列表
    @Override
    public Page<Comment> getListByUser(Map<String, Object> commentVO) {
        Integer currentPage = (Integer) commentVO.get("currentPage");
        Integer pageSize = (Integer) commentVO.get("pageSize");
        Example example = createExample(commentVO);
        PageHelper.startPage(currentPage, pageSize);
        Page<Comment> commentList = (Page<Comment>) commentMapper.selectByExample(example);
        return commentList;
    }

    //获取用户点赞的信息
    @Override
    public Page<Comment> getPraiseListByUser(long currentPage, long pageSize) {
        PageHelper.startPage((int) currentPage, (int) pageSize);
        Page<Comment> praiseListByUser = commentMapper.getPraiseListByUser();
        return praiseListByUser;
    }

    //关闭邮件通知
    @Override
    public void closeEmailNotification(String userUid) {
        commentMapper.closeConment(userUid);
    }

    //举报评论
    @Override
    public void report(Map<String, Object> map) {
        CommentReport report = new CommentReport();
        List<Comment> comments = commentMapper.getComment((String) map.get("blogUid"), (String) map.get("userUid"));
        if (comments!=null){
            for (Comment comment : comments) {
                String commentUid = comment.getUid();
                String userUid = comment.getUserUid();

                report.setUid(idWorker.nextId() + "");
                report.setUserUid((String) map.get("userUid"));          //举报用户
                report.setReportCommentUid(commentUid);  //被举报评论id
                report.setReportUserUid(userUid);   //被举报用户id
                report.setContent((String) map.get("content"));
                report.setProgress(1);
                report.setStatus(0);
                report.setCreateTime(new Date());
                report.setUpdateTime(new Date());
                commentReportMapper.insertSelective(report);
            }

        }


    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 唯一uid
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andLike("uid", "%" + searchMap.get("uid") + "%");
            }
            // 用户uid
            if (searchMap.get("userUid") != null && !"".equals(searchMap.get("userUid"))) {
                criteria.andLike("userUid", "%" + searchMap.get("userUid") + "%");
            }
            // 回复某条评论的uid
            if (searchMap.get("toUid") != null && !"".equals(searchMap.get("toUid"))) {
                criteria.andLike("toUid", "%" + searchMap.get("toUid") + "%");
            }

            // 回复某个人的uid
            if (searchMap.get("toUserUid") != null && !"".equals(searchMap.get("toUserUid"))) {
                criteria.andEqualTo("toUserUid", searchMap.get("toUserUid"));
            }

            // 评论内容
            if (searchMap.get("content") != null && !"".equals(searchMap.get("content"))) {
                criteria.andEqualTo("content", searchMap.get("content"));
            }

            // 博客uid
            if (searchMap.get("blogUid") != null && !"".equals(searchMap.get("blogUid"))) {
                criteria.andEqualTo("blogUid", searchMap.get("blogUid"));
            }

            // 状态
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }

            // 创建时间
            if (searchMap.get("createTime") != null) {
                criteria.andEqualTo("createTime", searchMap.get("createTime"));
            }

            // 更新时间
            if (searchMap.get("updateTime") != null) {
                criteria.andEqualTo("updateTime", searchMap.get("updateTime"));
            }

            // 评论来源
            if (searchMap.get("source") != null && !"".equals(searchMap.get("source"))) {
                criteria.andEqualTo("source", searchMap.get("source"));
            }

            // 评论类型
            if (searchMap.get("type") != null) {
                criteria.andEqualTo("type", searchMap.get("type"));
            }

            // 一级评论UID
            if (searchMap.get("firstCommentUid") != null && !"".equals(searchMap.get("firstCommentUid"))) {
                criteria.andEqualTo("firstCommentUid", searchMap.get("firstCommentUid"));
            }

        }
        return example;
    }
}
